#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  05-synFlood
# Version	:  1.0
# Date		:  2022/01/24
# Author	:  Lz
# Email		:  liuzuo@kylinos.com.cn 
# History	:     
#            	   Version 1.0, 2022/01/24
# Function	:  syn flood攻击 
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="安全加固(网络类) - syn flood攻击"


## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
	if [ "Z${SyncFlood_Network_SSRFlag}" == "Z" ];then
		OutputRet_LTFLIB "${ERROR}"	
		TestRetParse_LTFLIB "未定义变量 SyncFlood_Network_SSRFlag"
	elif [ "Z${SyncFlood_Network_SSRFlag}" != "ZTrue" -a "Z${SyncFlood_Network_SSRFlag}" != "ZFalse" ];then
		OutputRet_LTFLIB "${ERROR}"	
		TestRetParse_LTFLIB "SyncFlood_Network_SSRFlag 无效的参数: ${SyncFlood_Network_SSRFlag}"
	fi

        return $TPASS
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
        return $TPASS
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
        testcase_1
        return $TPASS
}


## TODO ： sysctl -a 命令查询
#
testcase_1(){
	local flag_str="net.ipv4.tcp_syncookies *= *"

	# 错误选项
        local false_str="${flag_str}"
	# 正确选项
	if [ "Z${SyncFlood_Network_SSRFlag}" == "ZTrue" ];then
        	local true_str="${flag_str}1$"
	else
        	local true_str="${flag_str}0$"
	fi

	# 查看文件中配置
	local file_conf="/etc/sysctl.conf"
	local cmd="cat ${file_conf}"
	eval ${cmd} | grep "$flag_str"
	eval ${cmd} | grep -q "${true_str}"
	CommRetParse_LTFLIB "${cmd}查询,要求${true_str}"

	eval ${cmd} | grep -v "${true_str}" | grep "${false_str}"
	CommRetParse_LTFLIB "${cmd}查询,不存在'${true_str}'以外的值" "True" "yes"

	# 执行sysctl -a 查看配置
	local cmd="sysctl -a 2>&1"
	eval ${cmd} | grep "$flag_str"
	eval ${cmd} | grep -q "${true_str}"
	CommRetParse_LTFLIB "${cmd}查询,要求${true_str}"

	eval ${cmd} | grep -v "${true_str}" | grep "${false_str}"
	CommRetParse_LTFLIB "${cmd}查询,不存在'${true_str}'以外的值" "True" "yes"
}


#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
